import requests
import json


class QianFan():
    """
    文心一言
    千帆大模型
    """

    API_KEY = '9L6eNjr304CWFVXx2QbEO0CA'
    SECRET_KEY = 'FdxcGeqg8EWLUcmLyiFNSosVO1jGGMvj'

    def __init__(self):
        self.token = self.get_access_token()

    def get_access_token(self):
        """
        使用 AK，SK 生成鉴权签名（Access Token）
        :return: access_token，或是None(如果错误)
        """
        url = "https://aip.baidubce.com/oauth/2.0/token"
        params = {
            "grant_type": "client_credentials",
            "client_id": self.API_KEY,
            "client_secret": self.SECRET_KEY
        }
        return str(
            requests.post(url, params=params).json().get("access_token"))

    def sendRequest(self, body):
        """
        发送请求
        """

        url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + self.token
        payload = json.dumps({
            "messages": [{
                "role": "user",
                "content": body + "\n这是分析师的观点，请告诉我认可度最高的下单点位和目标止盈止损位。"
            }]
        })
        headers = {'Content-Type': 'application/json'}
        response = requests.request("POST", url, headers=headers, data=payload)
        return json.loads(response.text)
